#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<functional>
#include<numeric>
class Solution {
public:
    int minElements(vector<int>& nums, int limit, int goal) {
        //先把nums全部求和
        double sum = accumulate(nums.begin(),nums.end(),0.0);
        double subValue = goal - sum;
        if(subValue==0){
            return 0;
        }
        return func(-1*limit,limit,subValue);

    }
    //在区间left和right中，最少的数相加等于target
    int func(int left,int right,double target){
        if(target>=left&&target<=right){
            return 1;
        }
        int sum = 0;
        while(target<left||target>right){
            if(target<left){
                int T = target/left;
                target -=T*(left*1.0);
                sum+=T;
            }
            else{
                int T = target/right;
                target -= T*(right*1.0);
                sum+=T;
            }
        }
        if(target!=0)
            return sum+1;
        else{
            return sum;
        }
    }
};

int main(){
    Solution S;
    vector<int> nums = {1,-1,1};
    cout<<S.minElements(nums,3,-4)<<endl;;
}